<template>
	<view class="wrapper code">
		<!--表单-->
		<view class="form">
			<uni-forms
				:modelValue="form"
				ref="formRef"
				:label-width="80"
				:rules="rules">
				<uni-forms-item label="推广码" name="code" required>
					<uni-easyinput
						type="text"
						:inputBorder="false"
						primaryColor="#e3230e"
						v-model="form.code"
						maxlength="6"
						placeholder="请输入推广码" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<button class="ty-button ty-button-confirm form-submit" :disabled="isSubmit" :loading="isSubmit" @click="submit">保存</button>
	</view>
</template>

<script setup>
import { computed, ref, watch, watchEffect } from 'vue';
import { onReady, onLoad, onReachBottom, onShow, onHide } from '@dcloudio/uni-app';
import useUserStore from '@/store/modules/user'
import useSetStore from '@/store/modules/set'
import { customerSpread } from "@/api/app_api/user"
import { throttle, debounce, test, sleep } from '@/uni_modules/uview-plus'
import modal from '@/plugins/modal'

	const userStore = useUserStore()
	const setStore = useSetStore()
	
	onLoad(() => {
		
	})
	
	// 表单
	const formRef = ref(null)
	const form = ref({
		code: ''
	})
	// 表单验证
	const rules = ref({
		code: {
			rules: [{
				required: true,
				errorMessage: '请输入推广码'
			}]
		}
	})
	
	// 表单提交
	const isSubmit = ref(false)
	const submit = () => {
		formRef.value.validate().then(formRes => {
			isSubmit.value = true
			const params = {
				...form.value
			}
			customerSpread(params).then(res => {
				uni.$u.toast(res.msg)
				uni.$u.sleep(1000).then(() => {
					isSubmit.value = false
					uni.navigateBack({
						delta: 1
					})
				})
			}).catch(err => {
				isSubmit.value = false
			})
		})
	}
	
	onReady(() => {
		formRef.value.setRules(rules.value)
	})
	
</script>

<style lang="scss">
	.code {
		padding: 20rpx;
	}
	.form {
		padding: 0rpx 32rpx;
		border-radius: 20rpx;
		background-color: #ffffff;
		&-submit {
			height: $button-height;
			padding: 0;
			margin: 0;
			margin-top: 40rpx;
			font-size: 32rpx;
		}
	}
</style>
